package com.qidian.mes.produce.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * @Author 陈俊成
 * @Company ccc
 * @Description
 * @Time 2024/10/17 10:54
 */
@Component
public class IDGenUtil {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public String genIfId(){
        StringBuilder sb = new StringBuilder();
        sb.append("PS");
        LocalDate localDate = LocalDate.of(2024,3,6);
        int year = localDate.getYear();
        sb.append(year);
        int monthValue = localDate.getMonthValue();
        sb.append(monthValue < 10 ? ("0" + monthValue) : monthValue);
        int dayOfMonth = localDate.getDayOfMonth();
        sb.append(dayOfMonth < 10 ? ("0" + dayOfMonth): dayOfMonth);
        // 顺位号
        String key = "PSID";
        String idStr = stringRedisTemplate.opsForValue().increment(key) + "";
        // 处理位数
        while(idStr.length() < 5){
            idStr = 0 + idStr;
        }
        sb.append(idStr);
        return sb.toString();
    }
}
